package com.neusoft.elm.view.Impl;

import java.util.List;
import java.util.Scanner;

import com.neusoft.elm.dao.BusinessDao;
import com.neusoft.elm.dao.impl.BusinessDaoImpl;
import com.neusoft.elm.po.Business;
import com.neusoft.elm.view.BusinessView;

public class BusinessViewImpl implements BusinessView {
	
	private Scanner input = new Scanner(System.in);

	@Override
	public void listBusinessAll() {
		BusinessDao dao = new BusinessDaoImpl();
		List<Business> list = dao.listBusiness(null,null);

		System.out.println("商家编号\t商家名称\t商家地址\t商家介绍\t起送費\t配送费");
		for(Business b : list){
			System.out.println(b.getBusinessId()+"\t"+b.getBusinessName()+"\t"
					+b.getBusinessAddress()+"\t"+b.getBusinessExplain()+"\t"
					+b.getStarPrice()+"\t"+b.getDeliveryPrice());
		}
	}

	@Override
	public void listBusiness() {
		String businessName = "";
		String businessAddress = "";
		
		String inputStr = "";
		System.out.println("是否需要输入商家名称关键词？（y/n）");
		inputStr = input.next();
		if (inputStr.equals("y")) {
			System.out.println("请输入商家名称关键字：");
			businessName = input.next();
			System.out.println("请输入商家地址关键字：");
			businessAddress = input.next();
		}
		
		BusinessDao dao = new BusinessDaoImpl();
		List<Business> list = dao.listBusiness(businessName,businessAddress);
		
		System.out.println("商家编号\t商家名称\t商家地址\t商家介绍\t起送費\t配送费");
		for(Business b : list){
			System.out.println(b.getBusinessId()+"\t"+b.getBusinessName()+"\t"
					+b.getBusinessAddress()+"\t"+b.getBusinessExplain()+"\t"
					+b.getStarPrice()+"\t"+b.getDeliveryPrice());
		}
	}

	@Override
	public void saveBusiness() {
		String businessName;
		int businessId ;
		
		System.out.println("请输入商家名称：");
		businessName = input.next();
		
		BusinessDao dao = new BusinessDaoImpl();
		businessId = dao.saveBusiness(businessName);
		if(businessId > 0){
			System.out.println("新建商家成功，商家编号为："
					+ businessId);
		}else{
			System.out.println("新建商家失败");
		}
		
	}
	
	@Override
	public void removeBusiness(){
		System.out.println("请输入商家编号：");
		int businessId = input.nextInt();
		BusinessDao dao = new BusinessDaoImpl();
		System.out.println("确认要删除吗？(y/n)");
		if(input.next().equals("y")){
			int result = dao.removeBusiness(businessId);
			if(result == 1){
				System.out.println("成功删除商家信息");
			}else {
				System.out.println("删除失败");
			}
		}
	}

	@Override
	public Business login() {
		System.out.println("请输入商家编号");
		int businessId = input.nextInt();
		System.out.println("请输入密码");
		String password = input.next();
		BusinessDao dao = new BusinessDaoImpl();
		
		Business b = dao.getBusinessByIdByPass(businessId, password);
		
		return b;
	}

	@Override
	public void showBusiness(Integer businessId) {
		BusinessDao dao = new BusinessDaoImpl();
		Business b = dao.showBusiness(businessId);
		System.out.println(b.toString());
	}

	@Override
	public void editBusiness(Integer businessId) {
		Business business = null;
		BusinessDao dao = new BusinessDaoImpl();
		business = dao.showBusiness(businessId);

		System.out.println(business.toString());
		
		String inputStr = "";
		System.out.println("是否修改商家名称？(y/n)");
		inputStr = input.next();
		if(inputStr.equals("y")){
			System.out.println("请输入新的商家名称");
			String str = input.next();
			business.setBusinessName(str);
		}
		
		System.out.println("是否修改商家地址？(y/n)");
		inputStr = input.next();
		if(inputStr.equals("y")){
			System.out.println("请输入新的商家地址");
			business.setBusinessAddress(input.next());
		}
		
		System.out.println("是否修改商家介绍？(y/n)");
		inputStr = input.next();
		if(inputStr.equals("y")){
			System.out.println("请输入新的商家介绍");
			business.setBusinessExplain(input.next());
		}
		
		System.out.println("是否修改起送费？(y/n)");
		inputStr = input.next();
		if(inputStr.equals("y")){
			System.out.println("请输入起送费");
			business.setStarPrice(input.nextDouble());
		}
		
		System.out.println("是否修改配送费？(y/n)");
		inputStr = input.next();
		if(inputStr.equals("y")){
			System.out.println("请输入配送费");
			business.setDeliveryPrice(input.nextDouble());
		}
		
		int result = dao.updateBusiness(business);
		
		if(result > 0){
			System.out.println("商家信息修改成功");
		}else {
			System.out.println("商家信息修改失败");
		}
	}

	@Override
	public void updateBusinessByPassword(Integer businessId) {
		
		BusinessDao dao = new BusinessDaoImpl();
		Business business = dao.showBusiness(businessId);

		System.out.println("请输入旧密码");
		String oldPass = input.next();
		
		if(oldPass.equals(business.getPassword())){
			int i = 3;
			while(i != 0){
				System.out.println("请输入新密码");
				String password = input.next();
				System.out.println("请再次输入新密码");
				String password2 = input.next();
				if(password.equals(password2)){
					int result = dao.updateBusinessByPassword(businessId, password);
					if(result > 0){
						System.out.println("修改密码成功");
					}else{
						System.out.println("修改密码失败");
					}
					i=0;
				}else{
					i--;
					System.out.println("两次密码输入不一致，还有"+i+"次机会");
				}
			}	
			
		}else{
			System.out.println("旧密码输入错误");
		}
	}

}
